package ar.com.android.restoapp.providers;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import ar.com.android.restoapp.dbHelpers.SQLFoodOrderHelper;
import ar.com.android.restoapp.entities.Food;

public class FoodProvider {
	private static final String TAG = "FoodProvider";
	
	private static final String DB_COLUMN_0_NAME = "idFood";
	private static final String DB_COLUMN_1_NAME = "description";
	private static final String DB_COLUMN_2_NAME = "idTypeFood";
	private static final String DB_COLUMN_3_NAME = "price";
	

	private SQLiteDatabase database;
	private SQLFoodOrderHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME, DB_COLUMN_3_NAME};
	
	public FoodProvider(Context context) {
		dbHelper = SQLFoodOrderHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertFood(Food food)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_0_NAME, food.getIdFood());
		contentValues.put(DB_COLUMN_1_NAME, food.getDescription());
		contentValues.put(DB_COLUMN_2_NAME, food.getIdTypeFood());
		contentValues.put(DB_COLUMN_3_NAME, food.getPrice());
		
		long insertId = database.insert(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeFood(Integer idFood)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idFood, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeFood()
	{
		int result = database.delete(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public long updateFood(Food food)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, food.getDescription());
		contentValues.put(DB_COLUMN_2_NAME, food.getIdTypeFood());
		contentValues.put(DB_COLUMN_3_NAME, food.getPrice());
		
		return database.update(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + food.getIdFood(), null);
	}
	
	public List<Food> getAllFood() 
	{
		
		Cursor cursor = null;
		List<Food> foods = new ArrayList<Food>();
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, allColumns, null, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				Food food = new Food();
				food.setIdFood(cursor.getInt(0));
				food.setDescription(cursor.getString(1));
				food.setIdTypeFood(cursor.getInt(2));
				food.setPrice(cursor.getDouble(3));
				
				foods.add(food);
				cursor.moveToNext();
			}
			return foods;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return foods;
		}
		finally {
			cursor.close();
		}
	}
	
	public List<Food> getAllFood(Integer idTypeFood)
	{
		Cursor cursor = null;
		List<Food> foods = new ArrayList<Food>();
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, allColumns, DB_COLUMN_2_NAME + " = " + idTypeFood, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				Food food = new Food();
				food.setIdFood(cursor.getInt(0));
				food.setDescription(cursor.getString(1));
				food.setIdTypeFood(cursor.getInt(2));
				food.setPrice(cursor.getDouble(3));
				
				foods.add(food);
				cursor.moveToNext();
			}
			return foods;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return foods;
		}
		finally {
			cursor.close();
		}
	}

	public Food getFood(Integer idFood) 
	{
		Cursor cursor = null;
		Food food = null;
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_FOOD_TABLE_NAME, allColumns, DB_COLUMN_0_NAME + " = " + idFood, null, null, null, null);
			cursor.moveToFirst();
			
			food = new Food();
			food.setIdFood(cursor.getInt(0));
			food.setDescription(cursor.getString(1));
			food.setIdTypeFood(cursor.getInt(2));
			food.setPrice(cursor.getDouble(3));
			
			cursor.close();
			return food;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return food;
		}
		finally {
			cursor.close();
		}
	}
}
